package com.example.juyuanapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.juyuanapp.bean.RegisterBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_register;
    private ImageView iv_back;
    private Button bt_tijiao;
    private EditText et_tel,et_pwd,et_repwd;
    private String telephone,password,repassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();

    }

    private void init() {
        tv_register=(TextView) findViewById(R.id.tv_register);
        tv_register.setText("手机注册");
        iv_back=(ImageView) findViewById(R.id.iv_back);
        bt_tijiao=(Button) findViewById(R.id.bt_tijiao);
        et_tel=(EditText) findViewById(R.id.et_tel);
        et_pwd=(EditText) findViewById(R.id.et_pwd);
        et_repwd=(EditText) findViewById(R.id.et_repwd);
        iv_back.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
        bt_tijiao.setOnClickListener(new  View.OnClickListener(){

            @Override
            public void onClick(View v) {
              if(et_pwd.getText().toString().equals(et_repwd.getText().toString())){
                  RequestQueue queue= Volley.newRequestQueue(RegisterActivity.this);//请求队列
                  String url="http://192.168.43.251:8080/user/register";
                  Map<String,String> map=new HashMap<>();
                  map.put("telephone",et_tel.getText().toString());
                  map.put("password",et_pwd.getText().toString());
                  JSONObject jsonObject=new JSONObject(map);
                  //创建新的请求
                  JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url,
                          jsonObject,
                          new Response.Listener<JSONObject>() {
                              @Override
                              public void onResponse(JSONObject jsonObject) {
                                  //解析后台代码
                                  Gson gson=new Gson();
                                  RegisterBean registerBean = gson.fromJson(jsonObject.toString(), RegisterBean.class);
                                  if(registerBean.isSuccess()){
                                      Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                      Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                                      startActivity(intent);
                                  }
                              }
                          },
                          new Response.ErrorListener() {
                              @Override
                              public void onErrorResponse(VolleyError volleyError) {
                                  Toast.makeText(RegisterActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                              }
                          });
                  queue.add(request);  //加入请求队列
              }else {
                  Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
              }
            }


        });
    }
}